<?php
namespace app\shop\controller\business;

use think\Controller;

class Address extends Controller
{

    // 用户模型
    protected $BusinessModel = null;
    // 收货地址模型
    protected $AddressModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('business.Business');
        $this->AddressModel = model('business.Address');

        $busid = $this->request->param('busid', '', 'trim');

        $business = $this->BusinessModel->find($busid);

        if (!$business) {
            $this->error('用户不存在!');
        }
    }

    public function add()
    {
        $params = $this->request->param();
        $business = $this->BusinessModel->find($params['busid']);

        if (!$business) {
            $this->error('用户不存在!');
        }

        // 开启事务
        $this->AddressModel->startTrans();

        $data = [
            'busid' => $params['busid'],
            'consignee' => $params['consignee'],
            'mobile' => $params['mobile'],
            'address' => $params['address']
        ];

        $status = $params['status'] ?? 0;

        if ($status == 1) {
            $this->AddressModel->where(['busid' => $params['busid']])->update(['status' => 0]);
        }

        $data['status'] = $status;

        $code = $params['code'] ?? '';

        if ($code) {
            $path = model('Region')->where(['code' => $code])->value('parentpath');

            $pathArr = explode(',', $path);

            $data['province'] = $pathArr[0] ?? null;
            $data['city'] = $pathArr[1] ?? null;
            $data['district'] = $pathArr[2] ?? null;
        }

        $result = $this->AddressModel->validate('commom/business/Address')->save($data);

        // 回滚事务
        if ($result === false) {
            $this->AddressModel->rollback();
            $this->error($this->AddressModel->getError());
        } else {
            $this->AddressModel->commit();
            $this->success('新增成功');
        }
    }

    public function index()
    {
        $busid = $this->request->param('busid', '', 'trim');
        $list = $this->AddressModel->where(['busid' => $busid])->order('id DESC')->select();

        if ($list) {
            $this->success('查询收货数据成功', null, $list);
        } else {
            $this->error("暂无收货地址");
        }
    }

    public function selected()
    {
        $params = $this->request->param();
        $address = $this->AddressModel->where(['id' => $params['id'], 'busid' => $params['busid']])->find();

        if (!$address) {
            $this->error('你不能修改他人默认的收货地址!');
        }

        // 开启事务
        $this->AddressModel->startTrans();

        $update = $this->AddressModel->where(['busid' => $params['busid']])->update(['status' => 0]);

        if ($update != true) {
            $this->error('收货地址状态更新失败');
        }

        $data = [
            'id' => $params['id'],
            'status' => 1
        ];

        $result = $this->AddressModel->isUpdate(true)->save($data);

        if ($result === false) {
            $this->AddressModel->rollback();
            $this->error('更新默认地址失败');
        } else {
            $this->AddressModel->commit();
            $this->success('切换默认地址成功');
        }
    }

    public function edit()
    {

    }
}